package com.example.demo;

import com.example.module.ListNode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class DemoController {

    @GetMapping("/")
    public String index() {
        return "首页";
    }


    public static void main(String[] args) {
        ListNode node = new ListNode();
        for (int i = 1; i < 5; i++) {
            node.addNode(node, i);
        }
        System.out.println(node);
    }


    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode node = new ListNode();
        while (l1.getNext() != null) {
            final int val1 = l1.getVal();
            final int val2 = l2.getVal();
            final int i = val1 + val2;
            if (i >= 10) {
                ListNode node1 = new ListNode();
                node1.setVal(1);
                node.addNode(node, i % 10);
            }
        }
        return node;
    }

    /**
     * int[] a = {3, 2, 4};
     * int target = 6;
     * <p>
     * int[] ints = twoSum(a, target);
     * System.out.println(ints);
     *
     * @param nums
     * @param target
     * @return
     */
    public static int[] twoSum(int[] nums, int target) {
        int[] indexs = new int[2];
        Map<Integer, Integer> hashMap = new HashMap<>(nums.length);
        for (int i = 0; i < nums.length; i++) {
            if (hashMap.containsKey(nums[i])) {
                //hashMap.put(nums[i],i);
                if (nums[i] * 2 == target) {
                    indexs[0] = hashMap.get(nums[i]);
                    indexs[1] = i;
                    return indexs;
                }
                continue;
            }
            hashMap.put(nums[i], i);
        }

        for (int i = 0; i < nums.length; i++) {
            int tempKey = target - nums[i];
            if (hashMap.containsKey(tempKey)) {
                if (i == hashMap.get(tempKey)) {
                    continue;
                }
                indexs[0] = i;
                indexs[1] = hashMap.get(tempKey);
                return indexs;
            }
        }

        return indexs;
    }


}
